#include "stm32f10x.h"
#include "key.h"
#include "tim.h"
#include "lcd.h"
#include "adc.h"
#include "stdio.h"

extern u8 clock_state;
extern u32 clock_set_time;
extern u8 shrink_flag;
extern u8 real_hour;
extern u8 real_min;
extern u8 real_sec;
extern u8 clock_hour;
extern u8 clock_min;
extern u8 clock_sec;
u32 TimingDelay = 0;
u8 alarm_flag = 1;
u8 led_flag = 1;

//Main Body
int main(void)
{
	char text[20] = {0};
	u16 temper = 0;
	SysTick_Config(SystemCoreClock/1000);
	STM3210B_LCD_Init();
  LCD_SetTextColor(White);
  LCD_SetBackColor(Blue2);
  LCD_Clear(Blue2);
	led_init();
	key_init();
	adc_init();
	tim2_init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	while(1)
	{
		deal_state();
		deal_alarm();
		temper = get_temper();
		led_ctrl(led_flag);
		
		switch(clock_state){
			case state_realTime_showing:{
				sprintf(text,"      Real Time    ");
				LCD_DisplayStringLine(Line2,text);
				sprintf(text,"      %d:%d:%d    ",real_hour,
																					real_min,
																					real_sec);
				LCD_DisplayStringLine(Line4,text);
			}break;
			case state_realTime_hour_setting:{
				if(shrink_flag == 0){
					sprintf(text,"      Real Time    ");
					LCD_DisplayStringLine(Line2,text);
					sprintf(text,"      %d:%d:%d    ",real_hour,
																					  real_min,
																						real_sec);
					LCD_DisplayStringLine(Line4,text);
				}
				if(shrink_flag == 0){
					sprintf(text,"      Real Time    ");
					LCD_DisplayStringLine(Line2,text);
					sprintf(text,"        ",real_min,
																						real_sec);
					LCD_DisplayStringLine(Line4,text);
				}
			}break;
			case state_realTime_min_setting:{
				if(shrink_flag == 0){
					sprintf(text,"      Real Time    ");
					LCD_DisplayStringLine(Line2,text);
					sprintf(text,"      %d:%d:%d    ",real_hour,
																						real_min,
																						real_sec);
					LCD_DisplayStringLine(Line4,text);
				}
				if(shrink_flag == 0){
					sprintf(text,"      Real Time    ");
					LCD_DisplayStringLine(Line2,text);
					sprintf(text,"      %d:  :%d    ",real_hour,
																						real_sec);
					LCD_DisplayStringLine(Line4,text);
				}
			}break;
			case state_realTime_sec_setting:{
				if(shrink_flag == 0){
					sprintf(text,"      Real Time    ");
					LCD_DisplayStringLine(Line2,text);
					sprintf(text,"      %d:%d:%d    ",real_hour,
																						real_min,
																						real_sec);
					LCD_DisplayStringLine(Line4,text);
				}
				if(shrink_flag == 0){
					sprintf(text,"      Real Time    ");
					LCD_DisplayStringLine(Line2,text);
					sprintf(text,"      %d:%d:      ", real_hour,
																						 real_min);
					LCD_DisplayStringLine(Line4,text);
				}
			}break;
			case state_temper_showing:{
				sprintf(text,"   Temperature    ");
				LCD_DisplayStringLine(Line2,text);
				sprintf(text,"       %dC         ",temper);
				LCD_DisplayStringLine(Line4,text);
			}break;
			case state_clockTime_hour_setting:{
				sprintf(text,"      Clock Time    ");
				LCD_DisplayStringLine(Line2,text);
				sprintf(text,"      %d:%d:%d    ",clock_hour,
																					clock_min,
																					clock_sec);
				LCD_DisplayStringLine(Line4,text);
			}break;
			case state_clockTime_min_setting:{
				sprintf(text,"      Clock Time    ");
				LCD_DisplayStringLine(Line2,text);
				sprintf(text,"      %d:%d:%d    ",clock_hour,
																					clock_min,
																					clock_sec);
				LCD_DisplayStringLine(Line4,text);
			}break;
			case state_clockTime_sec_setting:{
				sprintf(text,"      Clock Time    ");
				LCD_DisplayStringLine(Line2,text);
				sprintf(text,"      %d:%d:%d    ",clock_hour,
																					clock_min,
																					clock_sec);
				LCD_DisplayStringLine(Line4,text);
			}break;
		}
	}
}